<template>
  <div class="app-container animated fadeIn">
    <el-row :gutter="10" class="mb8">
      <el-col :span="22">
        <el-form inline>
          <el-form-item label="客户代码:">
            <HdSelectMaterial v-model="itmCustwldm" placeholder="请输入客户代码" keyName="itmCustwldm" @change="changeBcdm" />
          </el-form-item>
          <el-form-item label="日期:">
            <el-date-picker
              v-model="date"
              placeholder="查询日期"
              style="width: 200px"
              format="yyyy-MM-dd 星期dddd"
              autocomplete="off"
              :clearable="false"
              @change="changeBcdm"></el-date-picker>
          </el-form-item>
          <el-form-item label="班次代码:">
            <el-radio-group v-model="BcDm" size="large" style="padding-left: 15px" @change="changeBcdm">
              <el-radio label="白班"></el-radio>
              <el-radio label="夜班"></el-radio>
              <el-radio label="部装"></el-radio>
              <el-radio label="返工入库"></el-radio>
              <!-- <el-radio label="白班-A" />
              <el-radio label="白班-B" />
              <el-radio label="夜班-A" />
              <el-radio label="夜班-B" /> -->
            </el-radio-group>
          </el-form-item>
          <el-form-item label="记录查看:">
            <el-radio-group v-model="tableFlag" size="large" style="padding-left: 15px" @change="changeBcdm">
              <el-radio label="简要"></el-radio>
              <el-radio label="详细"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="warning" plain icon="el-icon-download" :loading="exportLoading" @click="handleExport" size="mini">导出</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="2"></el-col>
    </el-row>
    <div v-show="tableFlag != '详细'" style="position: relative">
      <div style="text-align: center">
        <h1 style="color: crimson; font-weight: bold; font-size: 32px">入库记录表</h1>
      </div>
      <div style="float: right; font-size: 18px; font-weight: bold; position: absolute; right: 32px">
        <span>时间:</span>
        <span>{{ curTime }}</span>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </div>
      <div class="rkpb">
        <table id="tabJy">
          <tr>
            <th style="width: 150px">客户编码</th>
            <th style="width: 120px">班次</th>
            <th style="width: 120px">仓库</th>
            <th :colspan="tabrkpb.cols">数量</th>
            <th style="width: 100px">合计</th>
          </tr>
          <tr v-for="row in tabrkpb.tab">
            <td>{{ row.rpdWldm }}</td>
            <td>{{ row.rpmBcdm }}</td>
            <td><dict-tag :options="allStorageOptions" :value="row.rpmCkdm" /></td>
            <td v-for="(n, i) in row.num" style="width: 80px" :key="i" :title="row.time[i]">
              {{ n }}
            </td>
            <td v-for="n in tabrkpb.cols - row.num.length">&emsp;</td>
            <td>{{ row.sum }}</td>
          </tr>
        </table>
      </div>
    </div>
    <div style="text-align: center" v-show="tableFlag == '详细'">
      <h1 style="color: crimson; font-weight: bold; font-size: 32px">入库详细记录表</h1>
      <br />
      <br />
      <el-table highlight-current-row :data="tableDetails" stripe border :height="600" :header-cell-style="{ 'text-align': 'center' }">
        <el-table-column label="#" type="index" width="40" align="center" />
        <el-table-column label="入库单号" :show-overflow-tooltip="true" align="center" prop="rpdDjbh" />
        <el-table-column label="二维码" :show-overflow-tooltip="true" align="center" prop="rrqId" />
        <el-table-column label="入库班次" :show-overflow-tooltip="true" align="center" prop="rpmBcdm" />
        <el-table-column label="客户编码" :show-overflow-tooltip="true" align="center" prop="itmCustwldm" />
        <el-table-column label="品名规格" :show-overflow-tooltip="true" align="center" prop="itmPmgg" />
        <el-table-column label="入库数量" :show-overflow-tooltip="true" align="center" prop="rpdRksl" />
        <el-table-column label="箱号" :show-overflow-tooltip="true" align="center" prop="rrqXs" />
        <el-table-column label="实际入库时间" :show-overflow-tooltip="true" align="center" prop="rpdRkDate" />
      </el-table>
    </div>
  </div>
</template>

<script>
import { getStatisticsWarehousing, exportWarehousingTable, getWarehousingTableDetail } from '@/api/repertory/PDA'
import { exportTable } from '@/utils/ruoyi'
import mixin from '@/mixins/common'

export default {
  name: 'WarehousingTable',
  mixins: [mixin.allStorageOptions],
  data() {
    return {
      // 导出遮罩层
      exportLoading: false,
      date: this.$moment().format('YYYY-MM-DD'),
      BcDm: '白班',
      itmCustwldm: '',
      tabrkpb: { cols: 1, tab: [] },
      tableDetails: [],
      tableFlag: '简要',
    }
  },
  created() {
    this.changeBcdm()
  },
  computed: {
    curTime() {
      return this.$moment(this.date).format('YYYY年M月D日') + ' ' + this.BcDm
    },
  },
  methods: {
    changeBcdm() {
      if (this.tableFlag == '详细') {
        this.handleDetail()
      } else {
        this.handleTotal()
      }
    },
    /** 查看简要数据 */
    handleTotal() {
      let query = this.createParams()
      getStatisticsWarehousing(query)
        .then((res) => {
          this.tabrkpb.cols = 1
          this.tabrkpb.tab = res.data
            .map((a) => {
              let b = a.number.split(',')
              this.tabrkpb.cols = Math.max(this.tabrkpb.cols, b.length)
              a.num = b
              a.sum = b.reduce((p, c) => p + parseInt(c), 0)
              a.time = a.totalksrq.split(',')
              return a
            })
            .sort((a, b) => a.rpdWldm.localeCompare(b.rpdWldm))
        })
        .catch((err) => {
          alert(err + '网络原因：请检查网络连接或服务器状态')
          console.log(err + '网络原因：请检查网络连接或服务器状态')
        })
    },
    /** 查看详细数据 */
    handleDetail() {
      let query = this.createParams()
      getWarehousingTableDetail(query).then((res) => {
        this.tableDetails = res.data
      })
    },
    /** 创建查询参数 */
    createParams() {
      let dat = this.$moment(this.date)
      dat = this.BcDm === '白班' ? dat.hour(9) : dat.hour(21)
      return {
        rpdRkDate: this.$moment(dat).format('YYYY-MM-DD HH:mm:ss'),
        rpmBcdm: this.BcDm,
        itmCustwldm: this.itmCustwldm,
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.tableFlag == '简要') {
        exportTable('#tabJy', '入库记录表')
        return
      }
      let query = this.createParams()
      exportWarehousingTable(query).then((res) => {
        this.download(res.msg)
      })
    },
  },
}
</script>

<style scoped lang="less">
.rkpb {
  margin: 32px 0px 0px 20px;
  table {
    table-layout: fixed;
    border: 1px solid black;
    border-collapse: collapse;
    text-align: center;
    line-height: 32px;
    font-size: 18px;
    td,
    th {
      border: 1px solid black;
    }
  }
}
</style>
